package com.neu.admin;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.neu.common.LoginUser;
import com.neu.common.RequiresPermissionsDesc;
import com.neu.common.validator.Order;
import com.neu.common.validator.Sort;
import com.neu.doc.AdminOrderController_detail;
import com.neu.doc.AdminOrderController_list;
import com.neu.doc.ResponseOK;
import com.neu.domain.ShoppingOrder;
import com.neu.service.AdminOrderService;
import com.neu.service.ShoppingOrderService;
import com.neu.util.ResponseUtil;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;

import javax.validation.constraints.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/order")
@Validated
public class AdminOrderController {
    private final Log logger = LogFactory.getLog(AdminOrderController.class);
    @Autowired
    private AdminOrderService adminOrderService;
    /**
     * 查询订单
     *
     * @param userId
     * @param orderSn
     * @param orderStatusArray
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @ApiOperation(value="获取订单(List)分页数据", notes="分页查询订单信息",response = AdminOrderController_list.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数", required = true, dataType = "integer"),
            @ApiImplicitParam(name = "limit", value = "每页多少条数据", required = true, dataType = "integer"),
            @ApiImplicitParam(name = "sort", value = "排序字段", required = true, dataType = "string"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "integer"),
            @ApiImplicitParam(name = "orderSn", value = "订单编号", required = false, dataType = "string")
    })
    @RequiresPermissions("admin:order:list")
    @RequiresPermissionsDesc(menu = {"商场管理", "订单管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(Integer userId, String orderSn,
            @RequestParam(required = false) List<Short> orderStatusArray,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @Sort @RequestParam(defaultValue = "id") String sort,
            @Order @RequestParam(defaultValue = "desc") String order) { 
    		 userId = 1;
    	return adminOrderService.list(userId, orderSn, orderStatusArray, page, limit, sort, order);
}


    /**
     * 订单详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value="获取订单详情", notes="获取订单详情",response = AdminOrderController_detail.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderid", value = "订单id", required = true, dataType = "integer")
    })
    @RequiresPermissions("admin:order:read")
    @RequiresPermissionsDesc(menu = {"商场管理", "订单管理"}, button = "详情")
//   TODO
    @GetMapping("/detail")
    public Object detail(@NotNull Integer orderId) {
   
    	return adminOrderService.detail(orderId);
    }

    /**
     * 订单退款
     *
     * @param body 订单信息，{ orderId：xxx }
     * @return 订单退款操作结果
     */
    @ApiOperation(value="订单退款", notes="订单退款",response = ResponseOK.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id", required = true, dataType = "integer"),
            @ApiImplicitParam(name = "refundMoney", value = "退款金额", required = true, dataType = "integer")
            
    })
    @RequiresPermissions("admin:order:refund")
    @RequiresPermissionsDesc(menu = {"商场管理", "订单管理"}, button = "订单退款")
    @PostMapping("/refund")
    public Object refund(@RequestBody String body) {
        return adminOrderService.refund(body);
    }
//  TODO

    /**
     * 发货
     *
     * @param body 订单信息，{ orderId：xxx, shipSn: xxx, shipChannel: xxx }
     * @return 订单操作结果
     */

//    @RequiresPermissions("admin:order:ship")
//    @RequiresPermissionsDesc(menu = {"商场管理", "订单管理"}, button = "订单发货")
//  TODO
    @ApiOperation(value="订单发货", notes="订单发货",response = ResponseOK.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id", required = true, dataType = "integer"),
            @ApiImplicitParam(name = "shipChannel", value = "快递公司", required = true, dataType = "string"),
            @ApiImplicitParam(name = "shipSn", value = "快递单号", required = true, dataType = "string")
            
    })
    @RequiresPermissions("admin:order:ship")
    @RequiresPermissionsDesc(menu = {"商场管理", "订单管理"}, button = "订单发货")
    @PostMapping("/ship")
    public Object ship(@RequestBody String body) {
        return adminOrderService.ship(body);
    }

}
